<?php

namespace app\wechat\controller;

use think\Config;
use think\Db;
use app\enumerations\SortTypeEnum;

class Finds extends Common {

    /**
     * 足迹列表
     * @return \think\response\Json
     */
    public function footprintList() {

        $currentCustomer = $this->request->currentCustomer;
        $timeSortType = $this->request->post("timeSortType");
        $pageNum = $this->request->post("pageNum");
        $pageSize = $this->request->post("pageSize");

        if (!checkEnumValue($timeSortType, SortTypeEnum::getAllList())) {
            return errorReturn('errorWechat.errorSortTypeValue');
        }

        $list = $this->footprintService
            ->getFootprintListForPage($currentCustomer['id'],
                $timeSortType, $pageNum, $pageSize);

        foreach ($list as &$val) {

            $val['nickname'] = strExceedingLength($val['nickname']);
            $val['newest_like_json'] = json_decode(
                $val['newest_like_json'], true);
            $val['date_time'] = formatTime(strtotime($val['update_time']));
            $val['background_image'] = $val['background_image']
                .config('constants.ossProcessResizeH');

            unset($val['update_time']);
        }

        $returnData['info']['headimgurl'] = $currentCustomer['headimgurl'];
        $returnData['list'] = $list;

        return successReturn($returnData);

    }

    /**
     * 人气指数列表
     * @return \think\response\Json
     */
    public function popularityList() {

        $currentCustomer = $this->request->currentCustomer;
        $pointSortType = $this->request->post("pointSortType");
        $pageNum = $this->request->post("pageNum");
        $pageSize = $this->request->post("pageSize");

        if (!checkEnumValue($pointSortType, SortTypeEnum::getAllList())) {
            return errorReturn('errorWechat.errorSortTypeValue');
        }

        $list = $this->customerService->getPopularityListForPage(
            $pointSortType, $pageNum, $pageSize);

        foreach ($list as &$val) {

            $val['nickname'] = strExceedingLength($val['nickname']);
            $val['background_image'] = $val['background_image']
                .config('constants.ossProcessResizeH');
            $val['newest_like_json'] = json_decode(
                $val['newest_like_json'], true);

            if (isNullOrEmpty($val['sign'])) {
                $val['sign'] = config('constants.defaultSign');
            }
        }

        $returnData['info']['headimgurl'] = $currentCustomer['headimgurl'];
        $returnData['list'] = $list;

        return successReturn($returnData);

    }

    /**
     * 附近的店列表
     * @return \think\response\Json
     */
    public function nearbyList() {

        $currentCustomer = $this->request->currentCustomer;
        $nearbySortType = $this->request->post("nearbySortType");
        $pageNum = $this->request->post("pageNum");
        $pageSize = 100;//$this->request->post("pageSize");
        $latitude = $this->request->post("latitude");
        $longitude = $this->request->post("longitude");

        if (!checkEnumValue($nearbySortType, SortTypeEnum::getAllList())) {
            return errorReturn('errorWechat.errorSortTypeValue');
        }

        // 修改用户位置信息
        $customerData['latitude'] = $latitude;
        $customerData['longitude'] = $longitude;
        $customerData['geohash'] = geohashEncode($latitude, $longitude);
        $this->customerService->updateByIdAndData($currentCustomer['id'], $customerData);

        $customerList = [];

        if($pageNum == 1) {
            $customerList = $this->customerService->getNearbyListForPage(
                $currentCustomer, $pageNum, $pageSize);
        }

        $nearbyCustomerList = getNearbyCustomerList($latitude,
            $longitude, $customerList, $nearbySortType);

        foreach ($nearbyCustomerList as &$val) {

            $val['nickname'] = strExceedingLength($val['nickname']);
            $val['distance'] = formatDistance($val['distance']);
            $val['background_image'] = $val['background_image']
                .config('constants.ossProcessResizeH');

            unset($val['latitude']);
            unset($val['longitude']);
        }

        $returnData['info']['headimgurl'] = $currentCustomer['headimgurl'];
        $returnData['list'] = $nearbyCustomerList;

        return successReturn($returnData);

    }

}